package alg20241119;

import java.util.Arrays;

public class Text {
    int[] tmp;
    public int[] sortArray(int[] nums) {
        tmp = new int[nums.length];
        marge(0, nums.length-1, nums);
        return nums;
    }
    public void marge(int left, int right, int[] nums){
        if(left >= right) return;
        int mid = (left + right) / 2;
        //分数组
        marge(left, mid, nums);
        marge(mid+1, right, nums);
        //归并数组
        int cur1 = left, cur2 = mid + 1, i = 0;
        while(cur1 <= mid && cur2 <= right){
            tmp[i++] = nums[cur1] <= nums[cur2] ? nums[cur1++] : nums[cur2++];
        }
        while(cur1 <= mid){
            tmp[i++] = nums[cur1++];
        }
        while(cur2 <= right){
            tmp[i++] = nums[cur2++];
        }
        for (int j = left; j <= right; j++) {
            nums[j] = tmp[j-left];
        }
    }

    public static void main(String[] args) {
        Text text  = new Text();
        int[] nums = {7,5,4,6};
        text.sortArray(nums);
        System.out.println(Arrays.toString(nums));
    }
}
